package com.atom.process.cnmuseum.bm.delegates;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.IdentityLinkType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.atom.common.VariablesConstant;
import com.atom.common.activiti.RunningTaskHandler;
import com.atom.process.cnmuseum.bm.dao.process.BmDbrwProcessDao;
import com.atom.process.cnmuseum.bm.dao.process.BmGcxxProcessDao;
import com.atom.process.cnmuseum.bm.dao.process.BmProcessDao;
import com.atom.process.cnmuseum.bm.dao.process.BmWhrlxxxProcessDao;
import com.atom.process.cnmuseum.bm.dao.process.BmWhsxProcessDao;
import com.atom.process.cnmuseum.bm.dao.process.BmXzxxProcessDao;
import com.atom.process.cnmuseum.bm.dao.process.BmYxxxProcessDao;
import com.atom.process.cnmuseum.bm.dao.task.BmDbrwTaskDao;
import com.atom.process.cnmuseum.bm.dao.task.BmGcxxTaskDao;
import com.atom.process.cnmuseum.bm.dao.task.BmTaskDao;
import com.atom.process.cnmuseum.bm.dao.task.BmWhrlxxxTaskDao;
import com.atom.process.cnmuseum.bm.dao.task.BmWhsxTaskDao;
import com.atom.process.cnmuseum.bm.dao.task.BmXzxxTaskDao;
import com.atom.process.cnmuseum.bm.dao.task.BmYxxxTaskDao;
import com.atom.process.cnmuseum.bm.entity.process.BmDbrwProcessDO;
import com.atom.process.cnmuseum.bm.entity.process.BmGcxxProcessDO;
import com.atom.process.cnmuseum.bm.entity.process.BmProcessDO;
import com.atom.process.cnmuseum.bm.entity.process.BmWhrlxxxProcessDO;
import com.atom.process.cnmuseum.bm.entity.process.BmWhsxProcessDO;
import com.atom.process.cnmuseum.bm.entity.process.BmXzxxProcessDO;
import com.atom.process.cnmuseum.bm.entity.process.BmYxxxProcessDO;
import com.atom.process.cnmuseum.bm.entity.task.BmDbrwTaskDO;
import com.atom.process.cnmuseum.bm.entity.task.BmGcxxTaskDO;
import com.atom.process.cnmuseum.bm.entity.task.BmTaskDO;
import com.atom.process.cnmuseum.bm.entity.task.BmWhrlxxxTaskDO;
import com.atom.process.cnmuseum.bm.entity.task.BmWhsxTaskDO;
import com.atom.process.cnmuseum.bm.entity.task.BmXzxxTaskDO;
import com.atom.process.cnmuseum.bm.entity.task.BmYxxxTaskDO;
import com.atom.process.cnmuseum.bm.tool.TaskDefinitions;

@Component("ManageCreateListener")
public class ManageCreateListener implements Serializable {

	@Resource RuntimeService runtimeService;
	@Resource RunningTaskHandler runningTaskHandler;

    @Resource
    BmGcxxProcessDao bmGcxxProcessDao;
    @Resource
    BmGcxxTaskDao bmGcxxTaskDao;

    @Resource
    BmProcessDao bmProcessDao;
    @Resource
    BmTaskDao bmTaskDao;

    @Resource
    BmXzxxProcessDao bmXzxxProcessDao;
    @Resource
    BmXzxxTaskDao bmXzxxTaskDao;
    
    @Resource
    BmYxxxProcessDao bmYxxxProcessDao;
    @Resource
    BmYxxxTaskDao bmYxxxTaskDao;
    

    @Resource
    BmWhrlxxxProcessDao bmWhrlxxxProcessDao;
    @Resource
    BmWhrlxxxTaskDao bmWhrlxxxTaskDao;
    

    @Resource
    BmDbrwProcessDao bmDbrwProcessDao;
    @Resource
    BmDbrwTaskDao bmDbrwTaskDao;
    

    @Resource
    BmWhsxProcessDao bmWhsxProcessDao;
    @Resource
    BmWhsxTaskDao bmWhsxTaskDao;
	/** serialVersionUID */
	private static final long serialVersionUID = 1L;

	public void execute(DelegateTask task) {

		String mainProcessInstanceId = (String) task.getVariable(VariablesConstant.MAIN_PROCESS_INSTANCE_ID);
		// 开始流程不用创建
		if (TaskDefinitions.编目.equal(task.getTaskDefinitionKey())
				&& mainProcessInstanceId == null) {
			return;
		}
		// 结束流程不用创建
		if (TaskDefinitions.资源审核.equal(task.getTaskDefinitionKey())) {
//			BmGcxxTaskDO lasttask = bmGcxxTaskDao.findLastByProcessId(mainProcessInstanceId);
//			String next = lasttask.getNext();
//			if("bm".equals(next)){// 采集审核后不生成新流程，直接归档
//				return;
//			}
		}
		// 把PDO的值赋给ADO
		PDO2ADO(mainProcessInstanceId, task.getId());

		
		// 写平台待办表
		Map<String, Object> variables = new HashMap<String, Object>();
		// TASK SCOPE VARS
		Map<String, Object> task_var = task.getVariables();
		variables.putAll(task_var);
		// EXECUTION SCOPE VARS
		Map<String, Object> exe_var = runtimeService.getVariables(mainProcessInstanceId);
		variables.putAll(exe_var);
		if (task.getAssignee() != null) {
			runningTaskHandler.insert(task, variables, task.getAssignee());
		} else {
			Set<IdentityLink> links = task.getCandidates();
			for (IdentityLink identityLink : links) {
				if (StringUtils.equals(IdentityLinkType.CANDIDATE,identityLink.getType())) {
					runningTaskHandler.insert(task, variables, identityLink.getUserId());
				}
			}
		}
	}

	private void PDO2ADO(String mainProcessInstanceId,String taskId){
		BmTaskDO ado = new BmTaskDO();
        BmProcessDO pdo = bmProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        BeanUtils.copyProperties(pdo, ado, "id");
        ado.setTaskId(taskId);
        bmTaskDao.save(ado);

        BmGcxxTaskDO gcado = new BmGcxxTaskDO();
        BmGcxxProcessDO gcpdo = bmGcxxProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        BeanUtils.copyProperties(gcpdo, gcado, "id");
        gcado.setTaskId(taskId);
        bmGcxxTaskDao.save(gcado);

        BmXzxxTaskDO xzado = new BmXzxxTaskDO();
        BmXzxxProcessDO xzpdo = bmXzxxProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        BeanUtils.copyProperties(xzpdo, xzado, "id");
        xzado.setTaskId(taskId);
        bmXzxxTaskDao.save(xzado);


        BmYxxxTaskDO yxado = new BmYxxxTaskDO();
        BmYxxxProcessDO yxpdo = bmYxxxProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        BeanUtils.copyProperties(yxpdo, yxado, "id");
        yxado.setTaskId(taskId);
        bmYxxxTaskDao.save(yxado);
        
        
        BmWhrlxxxTaskDO whrlxxxado = new BmWhrlxxxTaskDO();
        BmWhrlxxxProcessDO whrlxxxpdo = bmWhrlxxxProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        BeanUtils.copyProperties(whrlxxxpdo, whrlxxxado, "id");
        whrlxxxado.setTaskId(taskId);
        bmWhrlxxxTaskDao.save(whrlxxxado);
        
        
        List<BmDbrwProcessDO> dbrwpdo_list = bmDbrwProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        for(BmDbrwProcessDO dbrwpdo:dbrwpdo_list){        	
        	BmDbrwTaskDO dbrwado = new BmDbrwTaskDO();
        	BeanUtils.copyProperties(dbrwpdo, dbrwado, "id");
        	dbrwado.setTaskId(taskId);
        	bmDbrwTaskDao.save(dbrwado);
        }

        List<BmWhsxProcessDO> whsxpdo_list = bmWhsxProcessDao.findByProcessInstanceId(mainProcessInstanceId);
        for(BmWhsxProcessDO whsxpdo:whsxpdo_list){    
        	BmWhsxTaskDO whsxado = new BmWhsxTaskDO();
        	BeanUtils.copyProperties(whsxpdo, whsxado, "id");
        	whsxado.setTaskId(taskId);
        	bmWhsxTaskDao.save(whsxado);
        }
	    
	}
}
